package com.mhsoft.fenak;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.mhsoft.fenak.map.MyItemizedOverlay;
import com.mhsoft.fenak.utils.MyGeoClass;
import com.readystatesoftware.maps.OnSingleTapListener;
import com.readystatesoftware.maps.TapControlledMapView;

public class MainActivity extends MapActivity implements OnClickListener, OnCheckedChangeListener{
	
	private TapControlledMapView mapView;
	private MyGeoClass geoPoint;
	private MyItemizedOverlay itemizedOverlay;
	private List<Overlay> mapOverlays;
	private MapController mapController;
	
	private LinearLayout menu_parent;
	private View menu_firstView, menu_secondView, menu_secondViewHeader, menu_thirdView;
	
	private Button startSession_button, usePath_button, drawPath_button, settings_button
					, chatting_button, stopUpdates_button, manualUpadates_button, nameDescription_button
					, endSession_button, addMember_button, nearByPaths_button, search_button;
	
	private CheckBox public_chkBox;
	
	private boolean isFirstViewPrepared, isSecondViewPrepared, isThirdViewPrepared;
	
	private ListView members_listView;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        
        // the menu ...
        menu_parent = (LinearLayout) findViewById(R.id.menu_parent);
        
        menu_firstView = getLayoutInflater().inflate(R.layout.menu_layout1, null);
        menu_secondView = getLayoutInflater().inflate(R.layout.menu_layout2_members, null);
        menu_secondViewHeader = getLayoutInflater().inflate(R.layout.menu_layout2_header, null);
        menu_thirdView = getLayoutInflater().inflate(R.layout.menu_layout3, null);
        
        prepareMenuFirstView();
        
        // the map ...
        prepareMap();
        
        ArrayList<MyGeoClass> a = new ArrayList<MyGeoClass>();
        a.add(new MyGeoClass(29.496988, 27.729492));
        a.add(new MyGeoClass(30.221102, 29.245605));
        a.add(new MyGeoClass(30.883369, 26.312256));
        
        drawOnMap(a, Color.BLUE, "1st", "1ID");
        
        a = new ArrayList<MyGeoClass>();
        a.add(new MyGeoClass(30.883369, 26.312256));
        a.add(new MyGeoClass(29.238477, 29.619141));
        
        drawOnMap(a, Color.GREEN, "2st", "2ID");
    }
    
    private void prepareMap(){
		
		mapView = (TapControlledMapView) findViewById(R.id.mapView);
        
        mapView.setBuiltInZoomControls(true);
        
        mapController = mapView.getController();
        
        mapView.setOnSingleTapListener(new OnSingleTapListener() {		
			@Override
			public boolean onSingleTap(MotionEvent e) {
				itemizedOverlay.hideAllBalloons();
				return true;
			}
		});
		
		mapOverlays = mapView.getOverlays();
	}
    
    private void drawOnMap(ArrayList<MyGeoClass> pathPoints, int color, String pathTitle, String pathID){
    	
    	Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
        
        itemizedOverlay = new MyItemizedOverlay(drawable, mapView, this, pathPoints, color);
        
        itemizedOverlay.setShowClose(true);
		itemizedOverlay.setShowDisclosure(true);
		itemizedOverlay.setSnapToCenter(true);
		
        int c = 0;
        
		for (int i = 0; i < pathPoints.size(); i++) {
			
			c += 1;
			
			// for better performance ...
			if(c == 5){
				
				itemizedOverlay = new MyItemizedOverlay(drawable, mapView, this, pathPoints, color);
		        
		        itemizedOverlay.setShowClose(true);
				itemizedOverlay.setShowDisclosure(true);
				itemizedOverlay.setSnapToCenter(true);
				
				c = 0;
			}
			
			setPins(pathPoints.get(i).getLatitude(), pathPoints.get(i).getLongitude()
					, pathTitle, pathID);
		}
    }
    
    private void setPins(double lat, double longitude, String title, String id){
		
		geoPoint = new MyGeoClass(lat,longitude);
		
		OverlayItem overlayItem = new OverlayItem(geoPoint, title, id);
		
		itemizedOverlay.addOverlay(overlayItem);
		
		mapOverlays.add(itemizedOverlay);

//		mapController.animateTo(new MyGeoClass(29.973970,31.267090));
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	// ===============================================================================================================
	// onClick(View v) ===============================================================================================
	// ===============================================================================================================
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		
			case R.id.startSession_button:
			
				prepareMenuSecondView();
				
			break;
			
			case R.id.usePath_button:
				
				prepareMenuthirdView();
				
			break;
			
			case R.id.drawPath_button:
				
			break;
				
			case R.id.settings_button:
				
			break;
			
			case R.id.chatting_button:
			
			break;
			
			case R.id.stopUpdates_button:
				
			break;
			
			case R.id.manualUpdates_button:
				
			break;
			
			case R.id.nameDescription_button:
				
			break;
			
			case R.id.endSession_button:
				
			break;
			
			case R.id.nearByPaths_button:
				
			break;
			
			case R.id.search_button:
				
			break;
		}
	}

	// ===============================================================================================================
	// onCheckedChanged(CompoundButton buttonView, boolean isChecked)=================================================
	// ===============================================================================================================
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		
	}
	
	// ===============================================================================================================
	// prepareMenuFirstView() ========================================================================================
	// ===============================================================================================================
	private void prepareMenuFirstView(){

		menu_parent.removeAllViews();
		
		if(!isFirstViewPrepared){

			startSession_button = (Button) menu_firstView.findViewById(R.id.startSession_button);
			startSession_button.setOnClickListener(this);
			
			usePath_button = (Button) menu_firstView.findViewById(R.id.usePath_button);
			usePath_button.setOnClickListener(this);
			
			drawPath_button = (Button) menu_firstView.findViewById(R.id.drawPath_button);
			drawPath_button.setOnClickListener(this);
			
			settings_button = (Button) menu_firstView.findViewById(R.id.settings_button);
			settings_button.setOnClickListener(this);
			
			isFirstViewPrepared = true;
		}
		
        menu_parent.addView(menu_firstView);
	}
    
	// ===============================================================================================================
	// prepareMenuSecondView() =======================================================================================
	// ===============================================================================================================
	private void prepareMenuSecondView(){
		
		menu_parent.removeAllViews();
		
		if(!isSecondViewPrepared){

			usePath_button = (Button) menu_secondViewHeader.findViewById(R.id.usePath_button);
			usePath_button.setOnClickListener(this);
			
			drawPath_button = (Button) menu_secondViewHeader.findViewById(R.id.drawPath_button);
			drawPath_button.setOnClickListener(this);
			
			settings_button = (Button) menu_secondViewHeader.findViewById(R.id.settings_button);
			settings_button.setOnClickListener(this);
			
			chatting_button = (Button) menu_secondViewHeader.findViewById(R.id.chatting_button);
			chatting_button.setOnClickListener(this);
			
			stopUpdates_button = (Button) menu_secondViewHeader.findViewById(R.id.stopUpdates_button);
			stopUpdates_button.setOnClickListener(this);
			
			manualUpadates_button = (Button) menu_secondViewHeader.findViewById(R.id.manualUpdates_button);
			manualUpadates_button.setOnClickListener(this);
			
			nameDescription_button = (Button) menu_secondViewHeader.findViewById(R.id.nameDescription_button);
			nameDescription_button.setOnClickListener(this);
			
			public_chkBox = (CheckBox) menu_secondViewHeader.findViewById(R.id.public_checkBox);
			public_chkBox.setOnCheckedChangeListener(this);
			
			endSession_button = (Button) menu_secondViewHeader.findViewById(R.id.endSession_button);
			endSession_button.setOnClickListener(this);
			
			members_listView = (ListView) menu_secondView.findViewById(R.id.members_list);

			addMember_button = new Button(this);
			addMember_button.setText(R.string.addMember);
			addMember_button.setOnClickListener(this);
			
			members_listView.addHeaderView(menu_secondViewHeader);
			members_listView.addFooterView(addMember_button);
			
			members_listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"mohammad","Ali"}));
			
			isSecondViewPrepared = true;
		}
		
        menu_parent.addView(menu_secondView);
	}

	// ===============================================================================================================
	// prepareMenuthirdView() ========================================================================================
	// ===============================================================================================================
	private void prepareMenuthirdView(){

		menu_parent.removeAllViews();
		
		if(!isThirdViewPrepared){

			startSession_button = (Button) menu_thirdView.findViewById(R.id.startSession_button);
			startSession_button.setOnClickListener(this);
			
			drawPath_button = (Button) menu_thirdView.findViewById(R.id.drawPath_button);
			drawPath_button.setOnClickListener(this);
			
			settings_button = (Button) menu_thirdView.findViewById(R.id.settings_button);
			settings_button.setOnClickListener(this);
			
			nearByPaths_button = (Button) menu_thirdView.findViewById(R.id.nearByPaths_button);
			nearByPaths_button.setOnClickListener(this);
			
			search_button = (Button) menu_thirdView.findViewById(R.id.search_button);
			search_button.setOnClickListener(this);
			
			isThirdViewPrepared = true;
		}
		
        menu_parent.addView(menu_thirdView);
	}
	//0nK2ZCdymKLoBvDU2fqh4pa9q4I7urKnowUj01w
}